<template>
	<div>
		<div style="display: flex; justify-content: center;align-items: center;">
			<el-input size="mini" placeholder="输入电话号码" v-model="user.phone" style="width: 300px; padding: 50px 15px"></el-input>
			<el-button size="mini" type="primary" @click="getQuestionByPhone">密码找回</el-button>
		</div>
		<div style="display: flex; justify-content: center;align-items: center;flex-direction: column;">
			<el-input size="mini" placeholder="密保问题" v-model="user.question" :disabled="true" rows="3" type="textarea" style="width: 500px; padding-bottom: 50px;"></el-input>
			<el-input size="mini" placeholder="密保答案" v-model="user.answer" type="textarea" rows="6" style="width: 500px; padding-bottom: 10px;"></el-input>
			<el-button size="mini" type="infor" @click="submitQuestion">提交问题</el-button>
		</div>
		<el-dialog title="修改密码" :visible.sync="dialogFormVisible">
			<el-form :model="user">
				<el-form-item label="密码" :label-width="formLabelWidth">
					<el-input v-model="user.password" show-password autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item label="确认密码" :label-width="formLabelWidth">
					<el-input v-model="rePassword" show-password autocomplete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="dialogFormVisible = false">取 消</el-button>
				<el-button type="primary" @click="updatePassword">确 定</el-button>
			</div>
		</el-dialog>

	</div>
</template>

<script>
	export default {
		name: "Forgot",
		data() {
			return {
				user: {
					phone: "",
					question: "",
					answer: "",
					password: "",
				},
				rePassword: "",
				formLabelWidth: "150px",
				dialogFormVisible: false,
			}
		},
		methods: {
			updatePassword() {
				if (this.user.password && this.rePassword) {
					if (this.user.password == this.rePassword) {
						this.postRequest("/users/updatePassword/",this.user).then(res => {
							this.user.password = "";
							this.rePassword = "";
							//表单验证
							this.dialogFormVisible = false;
							this.user.phone = "";
							this.user.question = "";
							this.user.answer = "";
						})
					} else {
						return this.$message({
							message: '两次密码不一致',
							type: 'warning'
						});
					}
				}else{
					return this.$message({
						message: '请输入完整信息',
						type: 'warning'
					});
				}

			},
			getQuestionByPhone() {
				this.getRequest("/users/getUserMessageByPhone/?phone=" + this.user.phone).then(res => {
					this.user.question = res.obj.question;
				})
			},
			submitQuestion() {
				this.postRequest("/users/checkQuestion", this.user).then(res => {
					if (res.msg == "验证成功") {
						this.dialogFormVisible = true;
					}
				})
			}

		},
	}
</script>

<style>
</style>
